package com.fruit.controller.wap.farmwork;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpSession;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.fruit.pojo.farmwork.BaseOperationRecord;
import com.fruit.pojo.user.SysUser;
import com.fruit.service.farmwork.BaseOperationManager;
import com.fruit.util.DateUtil;
import com.fruit.util.ResultInfo;
import com.fruit.util.ResultUtil;

@Controller
@RequestMapping(value = "/wap/operation")
public class OperationController {
	private  Logger logger = LoggerFactory.getLogger(OperationController.class);
	@Autowired
	private BaseOperationManager baseOperationService;
	/**
	 * 新增维护记录
	 * @param record 维护记录
	 * @return Map<String, Object> 
	 */
	@RequestMapping(value = "/add", method = { RequestMethod.GET ,RequestMethod.POST })
	@ResponseBody
	public Map<String, Object> insert(BaseOperationRecord record,HttpSession session){
		logger.info("【新增维护记录】---【开始】--");
		Map<String, Object> result = new HashMap<String, Object>();
		try {
			SysUser sysUser =(SysUser)session.getAttribute("sysUser");			
			if(sysUser==null){
				result.put("result", ResultUtil.initResult(ResultInfo.ERROR, "param_error", "获取session失败!"));
				return result;
			}			
			if(record==null){
				result.put("result", ResultUtil.initResult(ResultInfo.ERROR, "param_error", "传入参数不能为空!"));
				return result;
			}
			logger.info("【新增维护记录】---【params】--" + record.toString());
			
			List<BaseOperationRecord> boList = new ArrayList<BaseOperationRecord>();
			
			if (record.getBaseAreaId() == 0 && "全部".equals(record.getOperationArea())) {
				logger.info("【新增维护记录】---【构建全部区域农事记录开始】--");
				Map<String, Object> pam = new HashMap<String, Object>();
				pam.put("baseId", sysUser.getBaseId());
				List<Map<String, Object>> regionList = baseOperationService.getRegionList(pam);
				
				for(Map<String, Object> obj:regionList){
					BaseOperationRecord rec = new BaseOperationRecord();				
					
					BaseOperationRecord parmas= new BaseOperationRecord();
					parmas.setBaseId(sysUser.getBaseId());
					parmas.setBaseAreaId(Integer.parseInt(""+obj.get("id")));
					List<Map<String, Object>> list=baseOperationService.selectAreaGoodsCountId(parmas);
					if(list !=null && list.size()==1){
						rec.setAreaGoodsCountId(Integer.parseInt(""+list.get(0).get("id")));
					}else{
						result.put("result", ResultUtil.initResult(ResultInfo.ERROR, "AreaGoodsCountId_error", "获取区域统计商品id异常!"));
						return result;
					}
					
					rec.setOperationType(record.getOperationType());
					rec.setOperationArea(""+obj.get("name"));
					rec.setOperationTime(record.getOperationTime());
					rec.setPictureVideo(record.getPictureVideo());
					rec.setRemark(record.getRemark());
					rec.setDescInfo(record.getDescInfo());
					rec.setBaseAreaId(Integer.parseInt(""+obj.get("id")));
					
					rec.setOperatorId(sysUser.getUserId());
					rec.setBaseId(sysUser.getBaseId());
					rec.setBaseName(sysUser.getBaseName());
					rec.setDelState("1");//有效
					rec.setShState("1"); //待审核
					rec.setCreateTime(DateUtil.getTime());
					boList.add(rec);
				}
			}else{
				BaseOperationRecord parmas= new BaseOperationRecord();
				parmas.setBaseId(sysUser.getBaseId());
				parmas.setBaseAreaId(record.getBaseAreaId());
				List<Map<String, Object>> list=baseOperationService.selectAreaGoodsCountId(parmas);
				if(list !=null && list.size()==1){
					record.setAreaGoodsCountId(Integer.parseInt(""+list.get(0).get("id")));
				}else{
					result.put("result", ResultUtil.initResult(ResultInfo.ERROR, "AreaGoodsCountId_error", "获取区域统计商品id异常!"));
					return result;
				}
				record.setOperatorId(sysUser.getUserId());
				record.setBaseId(sysUser.getBaseId());
				record.setBaseName(sysUser.getBaseName());
				record.setDelState("1");//有效
				record.setShState("1"); //待审核
				record.setCreateTime(DateUtil.getTime());
				boList.add(record);
			}
			
			if(boList.size()<=0){
				result.put("result", ResultUtil.initResult(ResultInfo.ERROR, "saveParams_error", "构建农事记录异常！"));
				return result;
			}
			baseOperationService.insertRecords(boList);
			logger.info("【新增维护记录】---【操作成功】--");
			result.put("result", ResultUtil.initResult(ResultInfo.SUCCESS, "ok", ""));
		} catch (Exception e) {
			logger.error("【新增维护记录】---【异常】---{}", e);
			result.put("result", ResultUtil.initResult(ResultInfo.ERROR, "exception", "系统异常"));
		}
		logger.info("【新增维护记录】---【结束】--");
		return result;
	}
	/**
	 * 修改维护记录
	 * @param record 维护记录
	 * @return Map<String, Object> 
	 */
	@RequestMapping(value = "/update", method = { RequestMethod.GET ,RequestMethod.POST })
	@ResponseBody
	public Map<String, Object> update(BaseOperationRecord record,HttpSession session){
		logger.info("【修改维护记录】---【开始】--");
		Map<String, Object> result = new HashMap<String, Object>();
		try {
			SysUser sysUser =(SysUser)session.getAttribute("sysUser");			
			if(sysUser==null){
				result.put("result", ResultUtil.initResult(ResultInfo.ERROR, "param_error", "获取session失败!"));
				return result;
			}
			if(record==null){
				result.put("result", ResultUtil.initResult(ResultInfo.ERROR, "param_error", "传入参数不能为空!"));
				return result;
			}
			logger.info("【修改维护记录】---【params】--" + record.toString());
			record.setOperatorId(sysUser.getUserId());
			record.setBaseId(sysUser.getBaseId());
			record.setBaseName(sysUser.getBaseName());
			record.setShState("1");// 1、待审核 
			
			int i = baseOperationService.updateByPrimaryKey(record);
			if (i <= 0){
				logger.info("【修改维护记录】---【操作失败】--影响记录数" + i);
				result.put("result", ResultUtil.initResult(ResultInfo.ERROR, "更新失败", ""));
			}else{
				logger.info("【修改维护记录】---【操作成功】--");
				result.put("result", ResultUtil.initResult(ResultInfo.SUCCESS, "ok", ""));
			}			
		} catch (Exception e) {
			logger.error("【修改维护记录】---【异常】---{}", e);
			result.put("result", ResultUtil.initResult(ResultInfo.ERROR, "exception", "系统异常"));
		}
		logger.info("【修改维护记录】---【结束】--");
		return result;
	}
	
	/**
	 * 删除维护记录(逻辑删除)
	 * @param record 维护记录
	 * @return Map<String, Object> 
	 */
	@RequestMapping(value = "/delete", method = { RequestMethod.GET ,RequestMethod.POST })
	@ResponseBody
	public Map<String, Object> delete(String id){
		logger.info("【删除维护记录】---【开始】--");
		Map<String, Object> result = new HashMap<String, Object>();
		try {
			if(StringUtils.isEmpty(id)){
				result.put("result", ResultUtil.initResult(ResultInfo.ERROR, "param_error", "传入参数不能为空!"));
				return result;
			}
			logger.info("【删除维护记录】---【params】--id=" + id);
			
			int i = baseOperationService.deleteByPrimaryKey(Long.parseLong(id));
			if (i <= 0){
				logger.info("【删除维护记录】---【操作失败】--影响记录数" + i);
				result.put("result", ResultUtil.initResult(ResultInfo.ERROR, "更新失败", ""));
			}else{
				logger.info("【删除维护记录】---【操作成功】--");
				result.put("result", ResultUtil.initResult(ResultInfo.SUCCESS, "ok", ""));
			}			
		} catch (Exception e) {
			logger.error("【删除维护记录】---【异常】---{}", e);
			result.put("result", ResultUtil.initResult(ResultInfo.ERROR, "exception", "系统异常"));
		}
		logger.info("【删除维护记录】---【结束】--");
		return result;
	}
	
	/**
	 * 查看维护记录
	 * @param record 维护记录
	 * @return Map<String, Object> 
	 */
	@RequestMapping(value = "/view", method = { RequestMethod.GET ,RequestMethod.POST })
	@ResponseBody
	public Map<String, Object> view(String id){
		logger.info("【查看维护记录】---【开始】--");
		Map<String, Object> result = new HashMap<String, Object>();
		try {
			if(StringUtils.isEmpty(id)){
				result.put("result", ResultUtil.initResult(ResultInfo.ERROR, "param_error", "传入参数不能为空!"));
				return result;
			}
			logger.info("【查看维护记录】---【params】--id=" + id);
			
			BaseOperationRecord res = baseOperationService.selectByPrimaryKey(Long.parseLong(id));
			logger.info("【查看维护记录】---【操作成功】--");
			result.put("result", ResultUtil.initResult(ResultInfo.SUCCESS, "ok", ""));
			result.put("data", res);//放入数据
		} catch (Exception e) {
			logger.error("【查看维护记录】---【异常】---{}", e);
			result.put("result", ResultUtil.initResult(ResultInfo.ERROR, "exception", "系统异常"));
		}
		logger.info("【查看维护记录】---【结束】--");
		return result;
	}
	
	/**
	 * 选择农事列表
	 * @return Map<String, Object> 
	 */
	@RequestMapping(value = "/farmWorkList", method = { RequestMethod.GET ,RequestMethod.POST })
	@ResponseBody
	public Map<String, Object> farmWorkList(){
		logger.info("【选择农事列表】---【开始】--");
		Map<String, Object> result = new HashMap<String, Object>();
		try {
			List<Map<String, Object>> farmWork = baseOperationService.getFarmWork();
			result.put("result", ResultUtil.initResult(ResultInfo.SUCCESS, "ok", ""));
			result.put("data", farmWork);//放入数据
		} catch (Exception e) {
			logger.error("【选择农事列表】---【异常】---{}", e);
			result.put("result", ResultUtil.initResult(ResultInfo.ERROR, "exception", "系统异常"));
		}
		logger.info("【选择农事列表】---【结束】--");
		return result;
	}
	
	/**
	 * 选择区域
	 * @param baseId 基地id
	 * @return Map<String, Object> 
	 */
	@RequestMapping(value = "/regionList", method = { RequestMethod.GET ,RequestMethod.POST })
	@ResponseBody
	public Map<String, Object> region(HttpSession session){
		logger.info("【选择区域列表】---【开始】--");
		Map<String, Object> result = new HashMap<String, Object>();
		try {
			SysUser sysUser =(SysUser)session.getAttribute("sysUser");			
			if(sysUser==null){
				result.put("result", ResultUtil.initResult(ResultInfo.ERROR, "param_error", "获取session失败!"));
				return result;
			}
			if(StringUtils.isEmpty(""+sysUser.getBaseId())){
				result.put("result", ResultUtil.initResult(ResultInfo.ERROR, "param_error", "传入参数不能为空!"));
				return result;
			}
			Map<String, Object> pam = new HashMap<String, Object>();
			pam.put("baseId", sysUser.getBaseId());
			List<Map<String, Object>> regionList = baseOperationService.getRegionList(pam);
			result.put("result", ResultUtil.initResult(ResultInfo.SUCCESS, "ok", ""));
			result.put("data", regionList);//放入数据
		} catch (Exception e) {
			logger.error("【选择区域列表】---【异常】---{}", e);
			result.put("result", ResultUtil.initResult(ResultInfo.ERROR, "exception", "系统异常"));
		}
		logger.info("【选择区域列表】---【结束】--");
		return result;
	}
	/**
	 * 获取系统时间
	 * @param baseId 基地id
	 * @return Map<String, Object> 
	 */
	@RequestMapping(value = "/getTime", method = { RequestMethod.GET ,RequestMethod.POST })
	@ResponseBody
	public Map<String, Object> getTime(HttpSession session){
		logger.info("【获取系统时间】---【开始】--");
		Map<String, Object> result = new HashMap<String, Object>();
		try {
			String time=DateUtil.getTime();
			result.put("data", time);
		} catch (Exception e) {
			logger.error("【获取系统时间】---【异常】---{}", e);
			result.put("result", ResultUtil.initResult(ResultInfo.ERROR, "exception", "系统异常"));
		}
		logger.info("【获取系统时间】---【结束】--");
		return result;
	}
}
